package edu.ncet.desktop.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.user.client.rpc.IsSerializable;

public class ResultModel implements IsSerializable, Serializable {

	private static final long serialVersionUID = 1347460974580125562L;
	
	List<CurrentExamResult> currResults = new ArrayList<CurrentExamResult>();
	List<HistoryResultModel> histResults = new ArrayList<HistoryResultModel>();
	
	public ResultModel(List<CurrentExamResult> currResults, List<HistoryResultModel> histResults)
	{
		this.currResults = currResults;
		this.histResults = histResults;
	}
	
	public ResultModel() 
	{
	}
	
	public void addCurrentResult(CurrentExamResult currResult)
	{
		getCurrResults().add(currResult);
	}
	
	public void addHistoryResult(HistoryResultModel histResult)
	{
		getHistResults().add(histResult);
	}

	public List<CurrentExamResult> getCurrResults() {
		return currResults;
	}

	public void setCurrResults(List<CurrentExamResult> currResults) {
		this.currResults = currResults;
	}

	public List<HistoryResultModel> getHistResults() {
		return histResults;
	}

	public void setHistResults(List<HistoryResultModel> histResults) {
		this.histResults = histResults;
	}
	
	public Integer getMarksforCurrentTypeNo(Integer typeNo)
	{
		int marks = 0;
		for(CurrentExamResult res : currResults)
		{
			if(res.typeNo == typeNo && res.getCorrectAns().equals(res.getWrittenAns()))
				++marks;
		}
		return marks;
	}
	
	public Integer getPercentageForCurrentTypeNo(Integer typeNo)
	{
		int marks = 0;
		int total = 0;
		for(CurrentExamResult res : currResults)
		{
			if(res.typeNo == typeNo)
				{
				if(res.getCorrectAns().equals(res.getWrittenAns()))
					++marks;
					++total;
				}
		}
		return (int)(((float)marks / total)*100) ;
	}
	
	public Integer getNumberOfQuestions(String type)
	{
		int count = 0;
		for(CurrentExamResult res : currResults)
		{
			if(res.getTypeName().equals(type))
				count++;
		}
		return count;
	}
	
	public Set<Integer> getCurrentTypeNos()
	{
		Set<Integer> results = new HashSet<Integer>();
		for(CurrentExamResult res : currResults)
		{
			results.add(res.getTypeNo());
		}
		return results;
	}
	
	public Integer getTypeNo(String typeName)
	{
		for(CurrentExamResult result : currResults)
		{
			if(result.typeName.equals(typeName))
				return result.typeNo;
		}
		return 0;
	}
	
	public Integer getHistoryPercentageForType(Integer typeNo)
	{
		for(HistoryResultModel histModel : histResults)
		{
			if(histModel.getTypeNo() == typeNo)
				return histModel.getPercentile();
		}
		return -1;
	}
}
